package relu.fbs.jfm;

import java.text.DecimalFormat;
import java.util.LinkedHashMap;

import jfm.model.Types.CropType;
import relu.fbs.DataValue;


public abstract class AbstractJFMCropData {	
	public final CropType cropType;
		
	public AbstractJFMCropData(CropType ct){
		cropType=ct;
	}
	public enum JFMCDatum {
		/** Area of this crop grown as normal ( ie not on set aside ) */
		AREA ("area"),
		/** Best estimate of the price obtained for this crop */
		PRICE ("price",true),
		/** Best estimate of the yield for this crop */
		YIELD("yield",true),
		/** Area of this crop under subsidy schemes */
		AREAENTITLEMENT("areaentitlement"),
		/** Subsidy payments per hectare for this crop */
		AREAPAYMENT("areapayment",true),
		/** Total enterprise output */
		EO("eo");
		
		public final String RName;
		public final boolean isIntensive;
		private JFMCDatum(String rn){RName=rn;isIntensive=false;};
		private JFMCDatum(String rn,boolean iI){RName=rn;isIntensive=iI;};
	}
	protected final LinkedHashMap<JFMCDatum,DataValue> data=new LinkedHashMap<JFMCDatum,DataValue>();
	
	public double getValue(JFMCDatum dt){
		return data.get(dt).autoValue();
	}
	
	/** Add a value to the weighted average. Works only if \c dt has isIntensive set to true */
	protected void addDatum(JFMCDatum dt,Double val,Double wgt){
		if ( !data.containsKey(dt)){
			throw new Error("cannot add value to datum "+dt+" because it does not exist");
		}
		data.get(dt).add(val,wgt);
	}
	
	/** Add a value to an unweighted sum. Works if \c dt has isIntensive set to false */
	protected void addDatum(JFMCDatum dt,Double val){
		if ( !data.containsKey(dt)){
			throw new Error("cannot add value to datum "+dt+" because it does not exist");
		}
		data.get(dt).add(val);
	}

	int addDataFromCSV(String[] sdata,int i) throws NumberFormatException {
		for(DataValue val:data.values()){
			val.add(Double.parseDouble(sdata[i]));
			i++;
		}
		return i;
	}
	/** Print data to csv */
	public  String allToCSV(DecimalFormat fmt){
		StringBuffer buff=new StringBuffer();

		for ( DataValue val:data.values()){
			buff.append(fmt.format(val.autoValue())+",");
		}
		buff.deleteCharAt(buff.length()-1); // Delete last comma

		return buff.toString();
	}
	/** Print header information for this crop */
	public static String allToCSVCropLabels(CropType ct){
		StringBuffer buff=new StringBuffer();
		for ( JFMCDatum label:JFMCDatum.values()){
			buff.append(ct+"."+label.RName+",");
		}
		buff.deleteCharAt(buff.length()-1); // Delete last comma
		return buff.toString();
	}
	
	/** Print header information for this crop */
	public static String allToCSVCropLabels(){
		StringBuffer buff=new StringBuffer();
		for ( JFMCDatum label:JFMCDatum.values()){
			buff.append(label.RName+",");
		}
		buff.deleteCharAt(buff.length()-1); // Delete last comma
		return buff.toString();
	}
		
}
